home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / QuickDraw / Record•RetrievePictInfo / Record•RetrievePictInfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-16  |  4.5 KB  |  190 lines  |  [TEXT/KAHL]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    Record•RetrievePictInfo                                    */
  4. /*                                                                            */
  5. /*    Description:    This snippet shows how to retrieve the colortable        */
  6. /*                    from multiple PICT resources using the System 7 calls    */
  7. /*                    RecordPictInfo and RetrievePictInfo.                    */
  8. /*                                                                            */
  9. /*    Files:            Record•RetrievePictInfo.π                                */
  10. /*                    Record•RetrievePictInfo.π.rsrc                            */
  11. /*                    Record•RetrievePictInfo.c                                */
  12. /*                                                                            */
  13. /*    Programmer:        Edgar Lee                                                */
  14. /*    Organization:    Apple Computer, Inc.                                    */
  15. /*    Department:        Developer Technical Support, DTS                        */
  16. /*    Language:        C (Think C version 5.0.2)                                */
  17. /*    Date Created:    10-20-92                                                */
  18. /*                                                                            */
  19. /****************************************************************************/
  20.  
  21. /* Constant Declarations */
  22.  
  23. #define    WWIDTH    512
  24. #define    WHEIGHT    200
  25.  
  26. #define    MAXPICTS    6        /* Maximum PICT resources used for the example. */
  27.  
  28. #define WLEFT    (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  29. #define WTOP    (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  30.  
  31. void initMac();
  32.  
  33. void createWindow();
  34. void retrieveColors();
  35. void doEventLoop();
  36.  
  37. main()
  38. {
  39.     initMac();
  40.     
  41.     createWindow();
  42.     
  43.     doEventLoop();
  44. }
  45.  
  46. void initMac()
  47. {
  48.     MaxApplZone();
  49.  
  50.     InitGraf( &thePort );
  51.     InitFonts();
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs( nil );
  56.     InitCursor();
  57.     FlushEvents( 0, everyEvent );
  58. }
  59.  
  60. void createWindow()
  61. {
  62.     Rect        rect;
  63.     WindowPtr    window;
  64.     
  65.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  66.     window = NewCWindow( 0L, &rect, "\pRecord•RetrievePictInfo", true, documentProc,
  67.                             (WindowPtr)-1L, true, 0L );                        
  68.     SetPort( window );
  69.     
  70.     TextMode( srcCopy );
  71.     TextFont( geneva );
  72.     TextSize( 9 );
  73.     
  74.     PenSize( 2, 1 );
  75. }
  76.  
  77. void retrieveColors()
  78. {
  79.     int                i;
  80.     RGBColor        color;
  81.     PicHandle        pict[MAXPICTS];
  82.     PictInfoID        pictInfoID;
  83.     PictInfo        pictInfo;
  84.     CTabHandle        ctable;
  85.     PaletteHandle    palette;
  86.     Rect            rect;
  87.     Str255            string;
  88.     
  89.     /* Request a unique ID for our PictInfo. */
  90.     NewPictInfo( &pictInfoID, returnColorTable, 256, popularMethod, 0 );
  91.     
  92.     MoveTo( 10, 30 );
  93.     DrawString( "\pRecording PictInfo" );
  94.     MoveTo( 10, 45 );
  95.     DrawString( "\pfrom PICT resources:" );
  96.     
  97.     /* Loop through the PICT resources. */
  98.     for (i = 0; i < MAXPICTS; i++)
  99.     {
  100.         /* Load the PICT. */
  101.         pict[i] = GetPicture( 128 + i );
  102.         HPurge( pict[i] );
  103.         
  104.         /* Define the PICT's destination rect then draw into it. */
  105.         rect = (**pict[i]).picFrame;
  106.         OffsetRect( &rect, -rect.left, -rect.top );
  107.         rect.right /= 2;
  108.         rect.bottom /= 2;
  109.         
  110.         OffsetRect( &rect, rect.right * (i + 2), 0 );
  111.         DrawPicture( pict[i], &rect );
  112.         
  113.         /* Draw the PICT's ID below its image. */
  114.         MoveTo( rect.left + 20, rect.bottom + 10 );
  115.         NumToString( (long)(128 + i), string );
  116.         DrawString( string );
  117.         
  118.         /* Accumulate information about this PICT. */
  119.         RecordPictInfo( pictInfoID, pict[i] );
  120.     }
  121.  
  122.     MoveTo( 10, 95 );
  123.     DrawString( "\pColortable returned from RetrievePictInfo:" );
  124.  
  125.     /* Retrieve the colortable from the recording. */
  126.     RetrievePictInfo( pictInfoID, &pictInfo, 256 );
  127.     ctable = pictInfo.theColorTable;
  128.     
  129.     /* Convert the colortable to a palette then attach it to the window. */
  130.     palette = NewPalette( 0, nil, pmTolerant + pmExplicit, 0 );
  131.     CTab2Palette( ctable, palette, pmTolerant + pmExplicit, 0 );
  132.     SetPalette( FrontWindow(), palette, false );
  133.     
  134.     /* Draw the colortable in the window to see what it looks like. */
  135.     for (i = 0; i < 256; i++)
  136.     {
  137.         color = (**ctable).ctTable[i].rgb;
  138.         
  139.         RGBForeColor( &color );
  140.         MoveTo( i * 2, 100 );
  141.         LineTo( i * 2, WHEIGHT );
  142.     }
  143.     
  144.     /* Release any used memory. */
  145.     DisposPictInfo( pictInfoID );
  146.     DisposeCTable( ctable );
  147.     DisposePalette( palette );
  148. }
  149.  
  150. void doEventLoop()
  151. {
  152.     EventRecord event;
  153.     WindowPtr   window;
  154.     short       clickArea;
  155.     Rect        screenRect;
  156.  
  157.     for (;;)
  158.     {
  159.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  160.         {
  161.             if (event.what == mouseDown)
  162.             {
  163.                 clickArea = FindWindow( event.where, &window );
  164.                 
  165.                 if (clickArea == inDrag)
  166.                 {
  167.                     screenRect = (**GetGrayRgn()).rgnBBox;
  168.                     DragWindow( window, event.where, &screenRect );
  169.                 }
  170.                 else if (clickArea == inContent)
  171.                 {
  172.                     if (window != FrontWindow())
  173.                         SelectWindow( window );
  174.                 }
  175.                 else if (clickArea == inGoAway)
  176.                     if (TrackGoAway( window, event.where ))
  177.                         return;
  178.             }
  179.             else if (event.what == updateEvt)
  180.             {
  181.                 window = (WindowPtr)event.message;    
  182.                 SetPort( window );
  183.                 
  184.                 BeginUpdate( window );
  185.                 retrieveColors();
  186.                 EndUpdate( window );
  187.             }
  188.         }
  189.     }
  190. }